package com.wongsir.springbootdemo.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wongsir.springbootdemo.constants.ErrorCode;
import com.wongsir.springbootdemo.constants.ResultMsg;
import com.wongsir.springbootdemo.constants.DemoHttpRequestConstants;
import com.wongsir.springbootdemo.service.ISpringbootDemoService;
import com.wongsir.springbootdemo.service.ICommonService;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @Description:接口的具体实现
 * @author hjd
 * @date 2018年8月13日
 */
@Service
@Slf4j
public class SpringbootDemoServiceImpl implements ISpringbootDemoService{

	@Autowired
	private ICommonService simpleCommonService;
	@Autowired
	private DemoHttpRequestConstants stConstants;
	
	@Override
	public String testLogin() {
		StringBuilder body = new StringBuilder();
		body.append("account=").append(stConstants.getAccount()).append("&password=").append(stConstants.getPassword());
		
		String httpResponse = simpleCommonService.login(stConstants.getLoginAddress(), body.toString());
		
		int status = JSONUtil.parseObj(httpResponse).getInt("status");
		String data = JSONUtil.parseObj(httpResponse).getStr("body");
		log.info("登录请求，http状态码：{}，返回消息体：{}",status,data);
		
		ResultMsg res = new ResultMsg();
		
		System.out.println(ErrorCode.SUCCESS.getValue());
		
		if(status == 200) {
			res.setResultInfo(ErrorCode.SUCCESS.getValue(), "登录成功", data);
		}else {
			res.setResultInfo(ErrorCode.FAIL.getValue(), "登录失败", data);
			log.error("登录失败，http状态码：{}，返回消息体：{}",status,data);
		}
		return JSONUtil.toJsonStr(res);
	}
	
}
